package com.shanzmoo.base.aop;

import com.shanzmoo.base.annotation.IntIn;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 *
 * @author: guizhao 2020/9/11 14:03
 */

/**
 * 自定义 数据集校验注解
 * @author: Gzhao 2020/12/14
 */
public class IntInValidator implements ConstraintValidator<IntIn, Integer> {

    private final Set<Integer> values = new HashSet<>();
    private String msg = null;
    private boolean allowNull;

    @Override
    public void initialize(IntIn annotation) {
        for (Integer value : annotation.values()) {
            this.values.add(value);
        }
        this.allowNull = annotation.allowNull();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if (null == value && allowNull) {
            return true;
        }
        boolean contains = values.contains(value);
        if (contains) {
            return true;
        }
        return false;
    }
}
